package samples.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import samples.spring.beans.User;
import samples.spring.beans.UserHolder;

/**
 * 使用java注解代替常用的xml来进行spring配置
 *
 * @see Configuration
 * @see Bean
 */
@Configuration
public class AnnotationMain {

    public static final Logger LOGGER = LoggerFactory.getLogger(AnnotationMain.class);

    public AnnotationMain(){
        LOGGER.info("Spring容器启动初始化");
    }

    @Bean
    public User user() {
        User user = new User();
        user.setName("test");
        user.setId(1);
        return user;
    }

    @Bean(name = "user2", initMethod = "init", destroyMethod = "destroy")
    @Scope("prototype")
    public User user2(){
        User user2 = new User();
        user2.setName("user2");
        user2.setId(2);
        return user2;
    }

    @Bean
    public UserHolder userHolder(User user){
        UserHolder userHolder = new UserHolder();
        userHolder.setUser(user);
        return userHolder;
    }
}
